var start = readConfigString('start');
logd(start);
if (start) {
    start = JSON.parse(start);
    deleteConfig("start");
} else {
    exit();
}
let s = 0;
if (start.data.hous > 0) {
    s = Math.round(start.data.hous * 3600);
}
var tid = thread.execAsync(function () {
    if (s > 0) {
        while (true) {
            sleep(2000);
            if (s >= 0) {
                s -= 2, logd(s);
            } else {
                logd('我完成任务了,再见');
                home();
                break;
            }
            douyin().tanchuang();
        }
    } else {
        while (true) {
            sleep(2000);
            logd('小金乌正在奔跑')
            douyin().tanchuang();
        }
    }
});
if (start.type == 'yanghao' && start.app == 'douyin') {
    utils.openApp("com.ss.android.ugc.aweme.lite");
    sleep(6000);
    logd(start.data.search);
    if(start.data.search){
       if(id('com.ss.android.ugc.aweme.lite:id/avg')){
        click(id('com.ss.android.ugc.aweme.lite:id/avg'));
        sleep(1000);
        let search = id('com.ss.android.ugc.aweme.lite:id/ai0').clz('android.widget.EditText').getOneNodeInfo(100);
        search.inputText(start.data.search);
        sleep(500);
        click(text('搜索').id('com.ss.android.ugc.aweme.lite:id/du2'));
        sleep(3000);
        let video = text('视频').id('android:id/text1').getOneNodeInfo(100);
        clickCenter(video.bounds);
        sleep(3000);
        let videoList = id('com.ss.android.ugc.aweme.lite:id/cu5').clz('android.widget.FrameLayout').getOneNodeInfo(100);
        clickCenter(videoList.bounds);
        }
    }
    douyin().start();
}

function douyin() {
    return {
        tanchuang() {
            if(has(id("com.ss.android.ugc.aweme.lite:id/beg"))){
                click(id("com.ss.android.ugc.aweme.lite:id/beg"));
                sleep(1000);
            }
            if(has(id("com.ss.android.ugc.aweme.lite:id/bb0"))){
                click(id("com.ss.android.ugc.aweme.lite:id/bb0"))
            }
            if (has(text('我知道了').id('com.ss.android.ugc.aweme.lite:id/e9i'))) {
                click(text('我知道了').id('com.ss.android.ugc.aweme.lite:id/e9i'));
            }
            if (has(text('暂时不要').id('com.ss.android.ugc.aweme.lite:id/e17'))) {
                click(text('暂时不要').id('com.ss.android.ugc.aweme.lite:id/e17'));
            }
            if (has(text('不再提示'))) {
                click(text('不再提示'));
            }
            if (has(id('com.ss.android.ugc.aweme.lite:id/alx'))) {
                click(id('com.ss.android.ugc.aweme.lite:id/alx'));
            }
        },
        start() {
            let i = 0;
                width = device.getScreenWidth(),
                height = device.getScreenHeight();
            while (true) {
                sleep(3000);
                if (s <= 0) {
                    home();
                    break;
                }
                // if (has(id("com.ss.android.ugc.aweme.lite:id/dbz").text("首页"))) {
                    i += 1;
                    logd(i + "次");
                    this.slideScreenDown(width / 2, height - 400, width / 2, 300, 300);
                    sleep(Math.round(Math.random() * 15 + 5) * 1000);//随机20秒
                    if (start.data.give && Math.round(Math.random() * 5 + 5) > 8) {//随机点赞
                        let x = Math.round(Math.random() * 300 + 300);
                        let y = Math.round(Math.random() * 300 + 300);
                        clickPoint(x, y);
                        sleep(200);
                        clickPoint(x, y);
                    } else if (start.data.follow && Math.round(Math.random() * 5 + 5) > 8) {
                        // 关注
                        if (id('com.ss.android.ugc.aweme.lite:id/aww').clz('android.widget.Button')) {
                            click(id('com.ss.android.ugc.aweme.lite:id/aww').clz('android.widget.Button'));
                        }
                    }
                    sleep(1000);
                    // if (Math.round(Math.random() * 5 + 5) > 8) {
                    //     // 评论
                    //     if (id('com.ss.android.ugc.aweme.lite:id/aa7').clz('android.widget.FrameLayout')) {
                    //         click(id('com.ss.android.ugc.aweme.lite:id/aa7').clz('android.widget.FrameLayout'));
                    //     }
                    // }
                // } else {
                //     sleep(3000);
                //     logd('不在首页')
                //     back();
                // }
            }
        },
        getcommit() {
            let selectors = id('com.ss.android.ugc.aweme.lite:id/a2m');
            if (has(selectors)) {
                let list = getNodeInfo(selectors, 10 * 1000);
                if (list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        var value = list[i];
                        arr.push(value.text);
                        if (i >= list.length - 1) {
                            let x1 = 522;
                            let y1 = 1684;
                            let x2 = 800;
                            let y2 = 223;
                            var result = swipeToPoint(x1, y1, x2, y2, 800);
                            if (result) {
                                toast("拖动成功");
                            } else {
                                toast("拖动失败");
                            }
                            if (has(text('暂时没有更多了'))) {
                                arr.forEach(item => {
                                    logd(item);
                                })
                                return;
                            } else {
                                getcommit();
                            }
                        }
                    }
                }
            }
        },
        slideScreenDown(startX, startY, endX, endY, pressTime) {
            swipeToPoint(startX, startY, endX, endY, pressTime);
        },
    }
}